package com.dorm.controller;

import com.dorm.entity.DisciplinaryRecord;
import com.dorm.service.DisciplinaryService;
import com.dorm.util.Debug;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/disciplinary-records")
@CrossOrigin(originPatterns = "*", allowedHeaders = "*", allowCredentials = "true")
public class DisciplinaryController {
    private final DisciplinaryService disciplinaryService;

    @Autowired
    public DisciplinaryController(DisciplinaryService disciplinaryService) {
        this.disciplinaryService = disciplinaryService;
    }

    @GetMapping
    public ResponseEntity<List<DisciplinaryRecord>> getAllRecords() {
        try {
            Debug.log("开始获取所有违纪记录");
            List<DisciplinaryRecord> records = disciplinaryService.getAllRecords();
            Debug.log("成功获取违纪记录，数量: " + records.size());
            return ResponseEntity.ok(records);
        } catch (Exception e) {
            Debug.error("获取违纪记录失败:", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                .header("X-Error-Message", e.getMessage())
                .build();
        }
    }

    @GetMapping("/student/{studentId}")
    public ResponseEntity<List<DisciplinaryRecord>> getStudentRecords(@PathVariable Long studentId) {
        try {
            List<DisciplinaryRecord> records = disciplinaryService.getStudentRecords(studentId);
            return ResponseEntity.ok(records);
        } catch (Exception e) {
            Debug.error("获取学生违纪记录失败:", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                .header("X-Error-Message", e.getMessage())
                .build();
        }
    }

    @PostMapping
    public ResponseEntity<DisciplinaryRecord> createRecord(@RequestBody DisciplinaryRecord record) {
        try {
            DisciplinaryRecord created = disciplinaryService.createRecord(record);
            return ResponseEntity.ok(created);
        } catch (Exception e) {
            Debug.error("创建违纪记录失败:", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                .header("X-Error-Message", e.getMessage())
                .build();
        }
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteRecord(@PathVariable Long id) {
        try {
            disciplinaryService.deleteRecord(id);
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            Debug.error("删除违纪记录失败:", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                .header("X-Error-Message", e.getMessage())
                .build();
        }
    }

    @GetMapping("/building/{buildingId}")
    public ResponseEntity<List<DisciplinaryRecord>> getByBuilding(@PathVariable Long buildingId) {
        try {
            Debug.log("获取宿舍楼违纪记录, buildingId: " + buildingId);
            List<DisciplinaryRecord> records = disciplinaryService.getByBuilding(buildingId);
            Debug.log("获取到违纪记录数量: " + records.size());
            return ResponseEntity.ok(records);
        } catch (Exception e) {
            Debug.error("获取宿舍楼违纪记录失败:", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                .header("X-Error-Message", e.getMessage())
                .build();
        }
    }
} 